/*
 * 
 * 
 * 
 */
package com.cms.controller.admin.model;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cms.Feedback;
import com.cms.FieldAttribute;
import com.cms.controller.admin.BaseController;
import com.cms.entity.Model;
import com.cms.entity.ModelField;
import com.cms.routes.RouteMapping;
import com.cms.util.FieldUtils;


/**
 * Controller - 模型字段
 * 
 * 
 * 
 */
@RouteMapping(url = "/admin/model/model_field")

public class ModelFieldController extends BaseController {
    
    /**
     * 检查名称是否存在
     */
    public void checkName() {
        Long modelId = getParaToLong("modelId");
        String name = getPara("name");
        if (StringUtils.isEmpty(name)) {
            renderJson(false);
        }
        renderJson(!new ModelField().dao().nameExists(modelId,name));
    }


	/**
	 * 添加
	 */
	public void add() {
		Long modelId = getParaToLong("modelId");
		setAttr("model", new Model().dao().findById(modelId));
		render(getView("model/model_field/add"));
	}

	/**
	 * 保存
	 */
	public void save() {
		Long modelId = getParaToLong("modelId");
		Model model = new Model().dao().findById(modelId);
		FieldAttribute fieldAttribute = getBean(FieldAttribute.class,"fieldAttribute",true);
		ModelField modelField = getModel(ModelField.class,"",true); 
		if(modelField.getIsEnabled()==null){
            modelField.setIsEnabled(false);
        }
        if(modelField.getIsRequired()==null){
            modelField.setIsRequired(false);
        }
        modelField.setSiteId(getCurrentSite().getId());
		modelField.setCreateDate(new Date());
		modelField.setModifyDate(new Date());
		modelField.setAttributeValue(JSONObject.toJSONString(fieldAttribute));
		modelField.save();
		//数据库表
		if(Model.TABLE_TYPE==model.getType()){
		    FieldUtils.add(model.getTableName(), modelField.getName(), modelField.getType());
		}
		redirect(getListQuery("/admin/model/model_field/list?modelId="+modelId));
	}

	
	/**
	 * 编辑
	 */
	public void edit() {
		Long id = getParaToLong("id");
		setAttr("modelField", new ModelField().dao().findById(id));
		render(getView("model/model_field/edit"));
	}

	/**
	 * 更新
	 */
	public void update() {
		FieldAttribute fieldAttribute = getBean(FieldAttribute.class,"fieldAttribute",true);
		ModelField modelField = getModel(ModelField.class,"",true); 
		ModelField pmodelField = new ModelField().dao().findById(modelField.getId());
		Model model = pmodelField.getModel();
		String tableName = model.getTableName();
		String oldFieldName = pmodelField.getName();
		String newFieldName = modelField.getName();
		String newFieldType = modelField.getType();
		if(modelField.getIsEnabled()==null){
		    modelField.setIsEnabled(false);
		}
		if(modelField.getIsRequired()==null){
		    modelField.setIsRequired(false);
		}
		modelField.setAttributeValue(JSONObject.toJSONString(fieldAttribute));
		modelField.setModifyDate(new Date());
		modelField.update();
		//数据库表
        if(Model.TABLE_TYPE==model.getType()){
            FieldUtils.update(tableName, oldFieldName,newFieldName, newFieldType);
        }
		redirect(getListQuery("/admin/model/model_field/list?modelId="+modelField.getModelId()));
	}
	
   /**
     * 修改排序
     */
    public void updateSort(){
        String sortArray = getPara("sortArray");
        JSONArray jsonArray = JSONArray.parseArray(sortArray);
        for(int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Long id = jsonObject.getLong("id");
            Integer sort = jsonObject.getInteger("sort");
            ModelField modelField = new ModelField().dao().findById(id);
            modelField.setSort(sort);
            modelField.update();
        }
        renderJson(Feedback.success(new HashMap<>()));
    }

	/**
	 * 列表
	 */
	public void list() {
		Long modelId = getParaToLong("modelId");
		List<ModelField> modelFields = new ModelField().dao().findList(modelId,null,getCurrentSite().getId());
		setAttr("modelFields", modelFields);
		setAttr("modelId", modelId);
		render(getView("model/model_field/list"));
	}

	/**
	 * 删除
	 */
	public void delete() {
		Long ids[] = getParaValuesToLong("ids");
		if(ArrayUtils.isNotEmpty(ids)){
			for(Long id:ids){
				ModelField modelField = new ModelField().dao().findById(id);
				Model model = modelField.getModel();
				//数据库表
		        if(Model.TABLE_TYPE==model.getType()){
		            FieldUtils.delete(model.getTableName(), modelField.getName());
		        }
		        modelField.delete();
			}
		}
		renderJson(Feedback.success(new HashMap<>()));
	}

}